import Api from './base-api'
import _ from 'lodash'

let baseApiUrl = null

// api调用。postParams 为Post时的params，paramsInPath为路径中的参数，并非接在?后面
let api = (path, method, data, paramsInPath, postParams) => {
  method = (method && method.toLowerCase()) || 'get'
  let urlToExtend = ''
  if (paramsInPath) {
    _.forEach(paramsInPath, param => {
      urlToExtend += '/' + encodeURI(param)
    })
  }
  return Api.http(`${_.startsWith(path, 'http') ? '' : baseApiUrl}${path}${urlToExtend}`, method, data, postParams)
}

// baseUrl 应该以 / 结尾
let initApi = (apiRespLogic, baseUrl, setConfigPreReq) => {
  baseApiUrl = baseUrl || ''
  Api.apiRespLogic = apiRespLogic
  Api.setConfig = setConfigPreReq
}

export {
  api, initApi
}
